﻿import {useBaseApi} from '/@/api/base';

// 区域预算配置表接口服务
export const useStoreBudgetConfigApi = () => {
	const baseApi = useBaseApi("storeBudgetConfig");
	return {
		// 分页查询区域预算配置表
		page: baseApi.page,
		// 查看区域预算配置表详细
		detail: baseApi.detail,
		// 新增区域预算配置表
		add: baseApi.add,
		// 更新区域预算配置表
		update: baseApi.update,
		// 删除区域预算配置表
		delete: baseApi.delete,
		// 批量删除区域预算配置表
		batchDelete: baseApi.batchDelete,
		// 导出区域预算配置表数据
		exportData: baseApi.exportData,
		// 导入区域预算配置表数据
		importData: baseApi.importData,
		// 下载区域预算配置表数据导入模板
		downloadTemplate: baseApi.downloadTemplate,
		// 获取下拉列表数据
		getDropdownData: (fromPage: Boolean = false, cancel: boolean = false) => baseApi.dropdownData({ fromPage }, cancel),
		
		// 额度管理相关接口
		// 更新预算使用情况
		updateBudgetUsage: (budgetId: number) => baseApi.request({
			url: baseApi.baseUrl + 'updateBudgetUsage',
			method: 'post',
			data: { id: budgetId }
		}),
		// 批量更新所有预算
		updateAllBudgetUsage: () => baseApi.request({
			url: baseApi.baseUrl + 'updateAllBudgetUsage',
			method: 'post'
		}),
		// 检查预算预警
		checkBudgetWarnings: () => baseApi.request({
			url: baseApi.baseUrl + 'checkBudgetWarnings',
			method: 'get'
		}),
		// 手动刷新预算
		refreshBudget: (id: number) => baseApi.request({
			url: baseApi.baseUrl + 'refreshBudget',
			method: 'post',
			data: { Id: id }
		}),
		// 手动刷新所有预算
		refreshAllBudgets: () => baseApi.request({
			url: baseApi.baseUrl + 'refreshAllBudgets',
			method: 'post'
		}),
		// 获取看板数据
		getDashboardData: (targetDate?: string) => baseApi.request({
			url: baseApi.baseUrl + 'getDashboardData',
			method: 'get',
			params: targetDate ? { targetDate } : undefined
		}),
		// 获取预算使用趋势
		getBudgetTrend: (days: number = 30) => baseApi.request({
			url: baseApi.baseUrl + 'getBudgetTrend',
			method: 'get',
			params: { days }
		}),
		// 获取区域当前预算
		getCurrentBudgetByOrg: (orgId: number, targetDate?: string) => baseApi.request({
			url: baseApi.baseUrl + 'getCurrentBudgetByOrg',
			method: 'get',
			params: { orgId, targetDate }
		}),
		// 获取审批用区域预算
		getBudgetForApproval: (orgId: number) => baseApi.request({
			url: baseApi.baseUrl + 'getBudgetForApproval',
			method: 'get',
			params: { orgId }
		}),
		// 获取区域预算历史
		getBudgetHistory: (orgId: number) => baseApi.request({
			url: baseApi.baseUrl + 'getBudgetHistory',
			method: 'get',
			params: { orgId }
		}),
		// 检查预算配置重叠
		checkBudgetConflicts: (data: CheckBudgetConflictInput) => baseApi.request({
			url: baseApi.baseUrl + 'checkBudgetConflicts',
			method: 'post',
			data
		}),
	}
}

// 区域预算配置表实体
export interface StoreBudgetConfig {
	// 主键Id
	id: number;
	// 区域ID
	orgsId?: number;
	// 预算开始时间
	startDate?: string;
	// 预算结束时间
	endDate?: string;
	// 总预算额度
	totalAmount?: number;
	// 已使用额度
	usedAmount: number;
	// 申请中额度
	pendingAmount: number;
	// 预警阈值
	warningThreshold: number;
	// 预算名称
	budgetName: string;
	// 是否启用预警
	isWarningEnabled: boolean;
	// 备注
	remark: string;
	// 租户Id
	tenantId: number;
	// 软删除
	isDelete?: boolean;
	// 创建时间
	createTime: string;
	// 更新时间
	updateTime: string;
	// 创建者Id
	createUserId: number;
	// 创建者姓名
	createUserName: string;
	// 修改者Id
	updateUserId: number;
	// 修改者姓名
	updateUserName: string;
}

// 预算汇总数据
export interface BudgetSummary {
	id: number;
	budgetName: string;
	orgsId: number;
	orgName: string;
	totalAmount: number;
	usedAmount: number;
	pendingAmount: number;
	totalUsed: number;
	usageRate: number;
	remainingAmount: number;
	warningThreshold: number;
	isWarningEnabled: boolean;
	status: 'normal' | 'warning' | 'danger';
	startDate: string;
	endDate: string;
}

// 预算总览汇总
export interface BudgetOverallSummary {
	totalBudgetCount: number;
	totalAmount: number;
	totalUsed: number;
	totalPending: number;
	totalRemaining: number;
	overallUsageRate: number;
	warningCount: number;
	dangerCount: number;
}

// 预算看板数据
export interface BudgetDashboard {
	summary: BudgetOverallSummary;
	budgetDetails: BudgetSummary[];
	lastUpdateTime: string;
	queryDate?: string;
}

// 预算趋势数据
export interface BudgetTrend {
	date: string;
	totalUsed: number;
	dayOfWeek: string;
}

// 预算历史数据
export interface BudgetHistory {
	id: number;
	budgetName: string;
	orgsId: number;
	orgName: string;
	totalAmount: number;
	usedAmount: number;
	pendingAmount: number;
	startDate: string;
	endDate: string;
	isActive: boolean;
	createTime: string;
}

// 预算冲突检查输入
export interface CheckBudgetConflictInput {
	orgsId: number;
	startDate: string;
	endDate: string;
	excludeId?: number;
}

// 预算冲突信息
export interface BudgetConflictInfo {
	id: number;
	budgetName: string;
	orgName: string;
	startDate: string;
	endDate: string;
	totalAmount: number;
}

// 预算冲突检查输出
export interface BudgetConflictCheckResult {
	hasConflicts: boolean;
	conflictCount: number;
	conflicts: BudgetConflictInfo[];
	suggestions: string[];
}